package org.black.phoneUtility.widget;

import java.io.File;
import java.util.Date;

import org.black.phoneUtility.R;

import android.content.Context;
import android.graphics.BitmapFactory;
import android.text.format.DateFormat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class FileAdapter extends ArrayAdapter<File> {
	private int resource;

	public FileAdapter(Context context, int resource) {
		super(context, resource);
		this.resource = resource;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder viewHolder = null;
		if (convertView == null) {
			LayoutInflater layoutInflater = (LayoutInflater) getContext()
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = layoutInflater.inflate(resource, null);
			viewHolder = new ViewHolder();
			viewHolder.imageView = (ImageView) convertView
					.findViewById(R.id.fileExplorerRowImageView);
			viewHolder.titleTextView = (TextView) convertView
					.findViewById(R.id.fileExplorerRowTitleTextView);
			viewHolder.summaryTextView = (TextView) convertView
					.findViewById(R.id.fileExplorerRowSummaryTextView);
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		File file = getItem(position);
		if (file.isDirectory() == true) {
			viewHolder.imageView.setImageBitmap(BitmapFactory.decodeResource(
					getContext().getResources(), R.drawable.folder));
		} else {
			viewHolder.imageView.setImageBitmap(BitmapFactory.decodeResource(
					getContext().getResources(), R.drawable.ic_launcher));
		}

		viewHolder.titleTextView.setText(file.getName());
		viewHolder.summaryTextView.setText(DateFormat.getDateFormat(
				getContext()).format(new Date(file.lastModified()))
				+ " "
				+ DateFormat.getTimeFormat(getContext()).format(
						new Date(file.lastModified())));

		return convertView;
	}

	static class ViewHolder {
		ImageView imageView;
		TextView titleTextView;
		TextView summaryTextView;
	}

}
